package cn.hongwei.nacos.service.impl;

import cn.hongwei.nacos.context.InstanceContext;
import cn.hongwei.nacos.context.InstanceRequest;
import cn.hongwei.nacos.context.InstanceResponse;
import cn.hongwei.nacos.handler.base.InstanceBaseHandler;
import cn.hongwei.nacos.handler.instance.InstanceListDetailHandler;
import cn.hongwei.nacos.handler.namespace.NameSpaceHandler;
import cn.hongwei.nacos.handler.servicelist.ServiceListHandler;
import cn.hongwei.nacos.param.NacosInfoParam;
import cn.hongwei.nacos.service.NacosInstancesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.NamespaceHandler;
import org.springframework.stereotype.Service;

/**
 * @author Great
 * @email 1224028994@qq.com
 */
@Service
public class NacosInstancesServiceImpl implements NacosInstancesService {

    @Autowired
    private NameSpaceHandler nameSpaceHandler;

    @Autowired
    private ServiceListHandler serviceListHandler;

    @Autowired
    private InstanceListDetailHandler instanceListDetailHandler;

    @Override
    public InstanceResponse obtainInstanceResponse(NacosInfoParam nacosInfoParam) {
        String ip = nacosInfoParam.getIp();
        Integer port = nacosInfoParam.getPort();
        InstanceRequest instanceRequest = new InstanceRequest();
        instanceRequest.setNacosInfoParam(NacosInfoParam.builder().ip(ip).port(port).build());
        InstanceBaseHandler.Builder builder = new InstanceBaseHandler.Builder();
        InstanceContext context= InstanceContext.builder().
                instanceRequest(instanceRequest).
                instanceResponse(InstanceResponse.builder().build()).build();

        builder.addHandler(nameSpaceHandler)
                .addHandler(serviceListHandler)
                .addHandler(instanceListDetailHandler)
                .build().doHandler(context);

        return context.getInstanceResponse();
    }
}
